<!-- "Mod/GeneralGameServerMod/View/UserInfo.html" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<pe:mcml>
<script refresh="false" type="text/npl" src="UserInfo.lua">
<![CDATA[
local UniString = commonlib.gettable("System.Core.UniString")
local UserInfo = commonlib.gettable("Mod.GeneralGameServerMod.App.View.UserInfo");
global_type = global_type or type;

function GetUserInfo() 
    return UserInfo;
end

-- GetUserInfo():SetPage(document:GetPageCtrl());

function Close() 
    UserInfo:Close();
end

function getProjectName(text)
    if global_type(text) ~= 'string' then
        return ''
    end

    local utf8Text = UniString:new(text)

    if _guihelper.GetTextWidth(text) > 112 then
        return utf8Text:sub(1, 8).text .. '...'
    else
        return text
    end
end

function getProjectUrl(id)
    -- return format("%s/pbl/project/%d/", KeepworkService:GetKeepworkUrl(), id or 0)
end

function GetImageStyle(extra)
    local url = extra and extra.imageUrl or "https://keepwork.com/public/img/project_default_cover_new.af774e7d.png"; 
    return string.format("width: 170px; height: 104px; background: url(%s);", url or "");
end

function GetCreateTime(datetime)
    local year, month, day = commonlib.timehelp.GetYearMonthDayFromStr(datetime);
    return tostring(year) .. "年" .. tostring(month) .. "月" .. tostring(day) .. "日";
end

function GetUpdatedTime(datetime)
    local year,month,day,hour,min,sec = string.match(datetime, "(%d+)%D(%d+)%D(%d+)%D+(%d+)%D(%d+)%D(%d+)");
    local dateTime = string.format("%s-%s-%s %s:%s:%s", year,month,day,hour,min,sec);
    local date,time = commonlib.timehelp.GetLocalTime();
    local curDateTime = string.format("%s %s", date, string.gsub(time, "-", ":"));
    local day,hours,minutes,seconds,time_str = commonlib.GetTimeStr_BetweenToDate(curDateTime, dateTime);
    local year = math.floor(day / 365);
    local month = math.floor(day / 30);
    if (year > 0) then
        return tostring(year) .. "年前";
    end
    if (month > 0) then
        return tostring(month) .. "月前";
    end
    if (day > 0) then
        return tostring(day) .. "天前";
    end
    return time_str;
end

function GetProjectList()
    return GetUserInfo().ProjectList or {};
end

-- 获取用户详情
function GetUserDetail()
    return UserInfo.UserDetail or {username = "", rank = {fans = 0, follow = 0}};
end

-- 注册时间
function GetRegisterTime()
    local datetime = GetUserDetail().createdAt;
    local year, month, day = commonlib.timehelp.GetYearMonthDayFromStr(datetime);
    return tostring(year) .. "年" .. tostring(month) .. "月" .. tostring(day) .. "日加入帕拉卡";
end

function GetWorksListDS(index)
    if(index) then
        return GetProjectList()[index]
    else
        return #GetProjectList();
    end
end

-- 是否关注
function IsFollow() 
    local isFollow = GetUserInfo().isFollow;
    if (global_type(isFollow) == "string") then
        return isFollow == "true";
    end
    return isFollow;
end

-- 点击打开世界
function ClickOpenWorld(worldId)
    GameLogic.RunCommand(string.format("/loadworld %d", worldId));    
end

-- 点击关注
function ClickFollow() 
    local Api = GetUserInfo():GetApi();
    if (not GetUserInfo():IsSignedIn()) then  return echo("未登录"); end
    local userId = GetUserDetail().id;
    local fansCount = GetFansCount();
    if (IsFollow()) then
        Api:HttpRequest({
            url = "/core/v0/favorites",
            method = "DELETE",
            qs = {
                objectType = 0,
                objectId = userId,
            },
        });
        GetUserInfo().isFollow = false;
        SetFansCount(fansCount - 1);
    else
        Api:HttpRequest({
            url = "/core/v0/favorites",
            method = "POST",
            form = {
                objectType = 0,
                objectId = userId,
            },
            json = true,
        });
        GetUserInfo().isFollow = true;
        SetFansCount(fansCount + 1);
    end
    GetUserInfo():Refresh();
end

-- 获取粉丝数
function GetFansCount()
    return GetUserDetail().rank.fans;
end

-- 设置粉丝数
function SetFansCount(count)
    GetUserDetail().rank.fans = count;
end

-- 是否登录
function IsSignedIn() 
    return GetUserInfo():IsSignedIn();
end

-- 是否关闭
function Close() 
    GetUserInfo():Close();
end

-- 启动初始化函数
-- GetUserInfo():Init(document:GetPageCtrl());
]]>
</script>
<style type="text/mcss">
</style>
<div style="width: 880px; height: 584px; background: url(Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png#267 89 34 34:8 8 8 8);">
    <div style="padding-left:18px; padding-top: 8px; padding-right:8px; height:36px">
        <div style="float: left; color:#ffffff; font-size: 16px; width: 100px;">用户信息</div>
        <input align="right" type="button" onclick="Close()" style="background:url(Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png#363 59 26 26:7 7 7 7); width:26px; height:26px;"/>
    </div>
    <div style="height:2px;" width="100%" class="mc_line"></div>
    <div style="height: 546px;"> 
        <!-- 左侧用户信息 -->
        <div style="float: left; width: 234px; color: white;">
            <div style="margin: -11px;">
                <pe:mc_player name="player" miniscenegraphname="AvatarMyselfTabCharacter" style="width:256px; height:256px;" isinteractive="false" />
            </div>
            <div style="color:white; text-align: center; font-size: 24px;"><%= GetUserDetail().username %></div>
            <div style="margin-top:16px; text-align: center; font-size: 16px; color: #969696;"><%= GetRegisterTime() %></div>
            <div style="margin-top: 60px; margin: 25px; ">
                <div style="float: left; width: 68px; text-align: center; width: 92px;">
                    <div style="font-size: 18px; color: #236dd7;"><%= GetUserDetail().rank.follow %></div>
                    <div style="font-size: 14px; color: #3d649b;">关注</div>
                </div>
                <div style="float: left; width: 68px; text-align: center; width: 92px;">
                    <div style="font-size: 18px; color: #236dd7;"><%= GetUserDetail().rank.fans %></div>
                    <div style="font-size: 14px; color: #3d649b;">粉丝</div>
                </div>
            </div>
            <input enabled="<%= IsSignedIn() %>" onclick="ClickFollow" align="center" type="button" value="<%=  IsFollow() and '已关注' or '关注' %>" style="margin-top: 37px; width: 184px; height: 40px; spacing: 20px; font-weight: bold; font-size: 18px;" class="mc_light_grey_button_with_fillet"/>
        </div>
        <!-- 中间线 -->
        <div style="float: left; width: 2px; height: 546px; margin-left: 0px; margin-top: 0px; background-color: #b7b2b2b;"></div>
        <!-- 右侧项目列表 -->
        <div style="float: left;">
            <pe:gridview  style="width: 644px; height: 546px;" CellPadding="10" DataSource="<%= GetWorksListDS %>" RememberScrollPos="true" DefaultNodeHeight="244" ItemsPerLine="3" AllowPaging="false">
                <Columns>
                    <div style="padding: 10px; width: 190px; height: 244px; background-color: #2c2f32; color:white;">
                        <div style="<%=GetImageStyle(Eval('extra'))%>"></div>
                        <div style="font-weight:bold; font-size:14px; margin-top: 12px;"><%=getProjectName(Eval("name"))%></div>
                        <div style="font-size:12px; color: #717171;margin-top: 5px;">创建时间: <%=GetCreateTime(Eval("createdAt"))%></div>
                        <div style="font-size:12px; color: #717171;margin-top: 5px;">上次更新: <%=GetUpdatedTime(Eval("updatedAt"))%></div>
                        <input name="<%= Eval('id') %>" onclick="ClickOpenWorld" type="button" style="margin-top: 12px; width: 170px; height: 25px; font-size: 8px;" class="mc_light_grey_button_with_fillet" value="访问世界" />
                    </div>
                </Columns>
                <!-- <PagerSettings Position="Bottom" height="1" />
                <PagerTemplate AutoHidePager="true">
                    <form>
                        <label name="page" style="height:18px;margin-left:59px;margin-top:-95px;color:#ffffff;" />
                        <input type="button" name="pre"  invisibleondisabled="false" zorder=2 animstyle="23" tooltip='<%=L"上一页"%>' style="margin-left:35px;margin-top:-93px;width:13px;height:15px;background:url(Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png#224 149 13 15);" Normal_BG="Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png#224 149 13 15" Pressed_BG="Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png;224 167 13 15" />
                        <input type="button" name="next" invisibleondisabled="false" zorder=2 animstyle="23" tooltip='<%=L"下一页"%>' style="margin-left:40px;margin-top:-93px;width:13px;height:15px;background:url(Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png#249 149 13 15);" Normal_BG="Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png;249 149 13 15" Pressed_BG="Texture/Aries/Creator/Theme/GameCommonIcon_32bits.png;249 167 13 15" />
                    </form>
                </PagerTemplate> -->
            </pe:gridview>
        </div>
    </div>
</div>
</pe:mcml>
</body>
</html>
<!-- 
    background-color: #ff0000;  不支持文本颜色 red
    height: 100% 不支持
    float: right 不支持
    margin: 不支持分开简写(10px 20px)
 -->